package com.thomas.controller.admin;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.thomas.entity.Blogger;
import com.thomas.service.BloggerService;
import com.thomas.util.CryptographyUtil;
import com.thomas.util.DateUtil;
import com.thomas.util.ResponseUtil;

/** 
 * @ClassName: BloggerAdminController 
 * @Description: 管理员博主Controller层 
 * @author thomas_liu  
 * @date 2017年5月15日 下午10:16:04 
 */
@Controller
@RequestMapping("/admin/blogger")
public class BloggerAdminController {
	
	@Autowired
	private BloggerService bloggerService; 
	
	/**
	 *查询博主信息
	 *@param response
	 *@throws Exception
	 */
	@RequestMapping("/find")
	public String find(HttpServletResponse response) throws Exception{
		Blogger blogger = bloggerService.find(1);
		JSONObject jsonObject = JSONObject.fromObject(blogger);
		ResponseUtil.write(response, jsonObject);
		return null;
	}
	
	/**
	 *修改博主信息 
	 *@param imageFile
	 *@param  
	 */
	@RequestMapping("/save")
	public String save(@RequestParam("imageFile") MultipartFile imageFile,Blogger blogger,HttpServletRequest request,HttpServletResponse response)throws Exception{
		if(!imageFile.isEmpty()){
			String filePath=request.getServletContext().getRealPath("/");
			String imageName= DateUtil.getCurrentDateStr()+"."+imageFile.getOriginalFilename().split("\\.")[1];
			imageFile.transferTo(new File(filePath+"static/userImages"+imageName));
			blogger.setImageName(imageName);
		}
		int resultTotal=bloggerService.update(blogger);
		StringBuffer result=new StringBuffer();
		if(resultTotal>0){
			result.append("<script language='javascript'>alert('修改成功!')</script>");
		}else{
			result.append("<script language='javascript'>alert('修改失败!')</script>");
		}
		ResponseUtil.write(response, result);
		return null;
	}
	
	/**
	 *修改博主密码
	 *@param newPassword
	 *@param response
	 *@return
	 *@throws Exception 
	 */
	@RequestMapping("/modifyPassword")
	public String modifyPassword(String newPassword,HttpServletResponse response) throws Exception{
		Blogger blogger = new Blogger();
		blogger.setPassword(CryptographyUtil.md5(newPassword, "thomas"));
		int resultTotal = bloggerService.update(blogger);
		JSONObject result=new JSONObject();
		if(resultTotal>0){
			result.put("success", true);
		}else{
			result.put("success", false);
		}
		ResponseUtil.write(response, result);
		return null;
	}
	
	/**
	 *注销
	 *@return 
	 *@throws Exception 
	 */
	@RequestMapping("/logout")
	public String logout() throws Exception{
		SecurityUtils.getSubject().logout();
		return "redirect:/login.jsp";
	}
	
}
